package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.DynamicMenuDao;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.DynamicMenuService;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@Service
public class DynamicMenuServiceImpl implements DynamicMenuService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private DynamicMenuDao dynamicMenuDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public LinkedHashSet<Menu> findMenuByUser(String username) {
        User user = userService.findByUsername(username);
        if (null==user){
            return null;
        }
        List<Role> roles = roleDao.selectByUserId(user.getId());
        LinkedHashSet<Menu> menus = new LinkedHashSet<>();
        //List<Menu> childrenMenus = new ArrayList<>();
        for (Role role : roles) {
            menus = dynamicMenuDao.selectFatherByRoleId(role.getId());
            for (Menu menu : menus) {
                List<Menu> childrenMenus = menuDao.selectChildrenByMenuId(menu.getId());
                menu.setChildren(childrenMenus);
            }
        }

        return menus;
    }
}
